/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.handeasy.session;

import com.handeasy.entity.CommentEntity;
import com.handeasy.entity.MarkerEntity;
import java.util.Date;
import java.util.List;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.ejb.Stateless;
import javax.jws.Oneway;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Amaury
 */
@WebService(serviceName = "CommentSessionBean")
@Stateless()
public class CommentSessionBean {

    @PersistenceContext(unitName="HandEasy-ejbPU")
    private EntityManager em;
    
    
    /**
     * Création d'un commentaire
     */
    @WebMethod(operationName = "createComment")
    public CommentEntity createComment(@WebParam(name = "lContent") String lContent, 
    @WebParam(name = "lLat") double lLat, 
    @WebParam(name = "lLong") double lLong, 
    @WebParam(name = "lAlert") Boolean lAlert) {
        CommentEntity comm = new CommentEntity();
        comm.setmContent(lContent);
        comm.setmDate(new Date());
        comm.setmAlert(lAlert);
        
        System.out.println("CREATECOMMENT " + lContent + " lLat= " + lLat + " lLong=" + lLong + " alert="+lAlert );
        
        Query q = em.createQuery("select m from MarkerEntity m where m.mLatitude='" + lLat + "' AND  m.mLongitude='" + lLong + "'");
        MarkerEntity marker = (MarkerEntity)q.getSingleResult();
        
        comm.setmMarker(marker);
        
        em.persist(comm);
        
        return comm;
    }
    
    
   
    /**
     * Web service operation
     */
    @WebMethod(operationName = "getCommentsFromLatLong")
    public List<CommentEntity> getCommentsFromLatLong(@WebParam(name = "latitude") Double latitude, @WebParam(name = "longitude") Double longitude) {
        //TODO write your implementation code here:
        System.out.println("select c from CommentEntity c where c.mMarker.id = (select m.id from MarkerEntity m where m.mLatitude = " + latitude + " AND m.mLongitude = " + longitude +")");
        
        Query q = em.createQuery("select c from CommentEntity c where c.mMarker.id  = (select m.id from MarkerEntity m where m.mLatitude = :latitude AND m.mLongitude = :longitude )");
        q.setParameter("latitude", latitude);
        q.setParameter("longitude", longitude);
        return q.getResultList();
        
    }
}
